<?php
set_time_limit(0);
if (isset($_GET['id']) && isset($_GET['id'])){
    include 'load.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {}
    $getid=(int)$_GET['id'];
    $fileName='./cache/share/'.date("Y-m-d").'.txt';
    $fileNew='./cache/share/'.date("Y-m-d").'-new.txt';
    if (1 == $getid){
        
    }else if (2 == $getid){
        $query=$mysqli->query("SELECT openid FROM `uinfo` WHERE `isold` LIKE 'old' ");
        $oldId=array();
        while ($list=$query->fetch_assoc()){
            $oldId[]=$list['openid'];
        }
        $query=$mysqli->query("SELECT openid FROM `orders` ");
        $newNum=$oldNum=0;
        while ($list=$query->fetch_assoc()){
            if (in_array($list['openid'], $oldId)){
                $oldNum++;
            }else{
                $newNum++;
            }
        }
        echo 'old:',$oldNum;
        echo "<br />";
        echo $newNum;
        echo "<br />";
        //var_dump($oldId);
        
        exit;
    }else if (3 == $getid){
        ini_set("display_errors", "On");
        error_reporting(E_ALL | E_STRICT);
        
        if (file_exists($fileName)){
            $shareInfo=json_decode(file_get_contents($fileName),true);
        }else{
            $query=$mysqli->query("SELECT * FROM `share` ");
            $shareInfo=array();
            while ($list=$query->fetch_assoc()){
                $isold='new';
                $queryUinfo=$mysqli->query("SELECT isold FROM `uinfo` where openid='".$list['openid']."' ");
                if($queryUinfo->num_rows>0){
                    $getUinfo=$queryUinfo->fetch_assoc();
                    $isold=$getUinfo['isold'];
                }
                $shareInfo[]=array('z'=>$list['zuopenid'],'i'=>$isold);
            }
            file_put_contents($fileName, json_encode($shareInfo));
        }
        if (file_exists($fileNew)){
            $newUserInfo=json_decode(file_get_contents($fileNew),true);
        }else{
            $query=$mysqli->query("SELECT openid FROM `uinfo` WHERE `isold` LIKE 'old' ");
            $oldId=array();
            while ($list=$query->fetch_assoc()){
                $oldId[]=$list['openid'];
            }
            $query=$mysqli->query("SELECT openid FROM `orders` ");
            $newUserInfo=array();
            while ($list=$query->fetch_assoc()){
                if (!in_array($list['openid'], $oldId)){
                    $newUserInfo[]=$list['openid'];
                }
            }
            file_put_contents($fileNew, json_encode($newUserInfo));
        }
        $allNew=$oldNew=$newNew=0;
        
        foreach ($newUserInfo as $openid){
            //echo "INSERT INTO `orders_tong` (`openid`) VALUES ('".$openid."');";
            //echo "<br />";
        }
    
        exit;
    }else if (4 == $getid){
        
        $shareInfo=json_decode(file_get_contents($fileName),true);
        
        $query=$mysqli->query("SELECT * FROM `orders_tong` WHERE `newlevel` IS NULL limit 0,1000");
        if($query->num_rows>0){
            while ($list=$query->fetch_assoc()){
                 $newlevel=1;
                 foreach ($shareInfo as $v){
                     if ($v['z'] == $list['openid']){
                         if ($v['i'] == 'old'){
                             $newlevel=2;
                         }else{
                             $newlevel=3;
                         }
                     }
                 }
                
                 $sql="update orders_tong set newlevel='".$newlevel."' where id='".$list['id']."' ";
                 $mysqli->query($sql);
                 $rand=rand(1,300);
                 if (1 == $rand){
                     echo $sql;
                     echo "<br />";
                 }
                 
                 
            }
 
            
            echo '<script>
window.setTimeout(function(){
	window.location.href="https://elixir.omnimkt.com/elixir07/web/?c=tong&id=4";
}, 100);
</script>';
        }
        
         
        exit;
    }else if (5 == $getid){
 
        $query=$mysqli->query("SELECT COUNT(id) AS count FROM `orders_tong` WHERE `newlevel` = 1");
        $getCountA=$query->fetch_assoc();
        echo "纯新注册：",$getCountA['count'];
        echo "<br />";
        
        $query=$mysqli->query("SELECT COUNT(id) AS count FROM `orders_tong` WHERE `newlevel` = 2");
        $getCountB=$query->fetch_assoc();
        echo "老带新注册：",$getCountB['count'];
        echo "<br />";
        
        
        $query=$mysqli->query("SELECT COUNT(id) AS count FROM `orders_tong` WHERE `newlevel` = 3");
        $getCountC=$query->fetch_assoc();
        echo "新带新注册：",$getCountC['count'];
        echo "<br /><br /><br />";
        
        echo "新注册总数：",($getCountA['count']+$getCountB['count']+$getCountC['count']);
        
        exit;
    }
    
    
    $riqiStart=12;
    
     
    for ($i=$riqiStart; $i<=31;$i++){
        $queryDay='2020-07-'.$i;
        echo "当前时间：",$queryDay;
        echo "<br />";
        
        $query=$mysqli->query("select count(distinct openid) as count from users where createtime like '%".$queryDay."%' ");
        $getData=$query->fetch_assoc();
        echo '实际参与用户数：'.$getData['count'];
        echo "<br />";
        
        $query=$mysqli->query("SELECT count(id) as count FROM `uinfo` WHERE `isold` LIKE 'new' and createtime like '%".$queryDay."%' ");
        $getData=$query->fetch_assoc();
        echo '新用户：'.($getData['count']-1);
        echo "<br />";
        
        $query=$mysqli->query("SELECT count(id) as count FROM `uinfo` WHERE `isold` LIKE 'old' and createtime like '%".$queryDay."%' ");
        $getData=$query->fetch_assoc();
        echo '老用户：'.$getData['count'];
        echo "<br />";
        
        $query=$mysqli->query("SELECT count(id) as count FROM `orders` WHERE `isshot` = 1 and createtime like '%".$queryDay."%' ");
        $getData=$query->fetch_assoc();
        echo '订单数：'.$getData['count'];
        echo "<br /><br /><br />";
        
    }
    
    $month=8;
    $currentDay=3;
    
    if ($month == 8){
        for ($i=1; $i<=$currentDay;$i++){
            $zeroI=$i;
            if ($i<10){
                $zeroI='0'.$i;
            }
            $queryDay='2020-0'.$month.'-'.$zeroI;
            echo "当前时间：",$queryDay;
            echo "<br />";
            
            $query=$mysqli->query("select count(distinct openid) as count from users where createtime like '%".$queryDay."%' ");
            $getData=$query->fetch_assoc();
            echo '实际参与用户数：'.$getData['count'];
            echo "<br />";
            
            $query=$mysqli->query("SELECT count(id) as count FROM `uinfo` WHERE `isold` LIKE 'new' and createtime like '%".$queryDay."%' ");
            $getData=$query->fetch_assoc();
            echo '新用户：'.($getData['count']-1);
            echo "<br />";
            
            $query=$mysqli->query("SELECT count(id) as count FROM `uinfo` WHERE `isold` LIKE 'old' and createtime like '%".$queryDay."%' ");
            $getData=$query->fetch_assoc();
            echo '老用户：'.$getData['count'];
            echo "<br />";
            
            $query=$mysqli->query("SELECT count(id) as count FROM `orders` WHERE `isshot` = 1 and createtime like '%".$queryDay."%' ");
            $getData=$query->fetch_assoc();
            echo '订单数：'.$getData['count'];
            echo "<br /><br /><br />";
            
        }
    }
    
    
    
 
}
 
   